package com.app.adnroid.sichuanxiaoke.cn.xiaokebusiness.activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import com.app.adnroid.sichuanxiaoke.cn.xiaokebusiness.R;
import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.model.LatLng;

public class BaiduMapActivity extends AppCompatActivity {
    MapView mapView;
    private BaiduMap baiduMap;
    private LocationClient locationClient;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SDKInitializer.initialize(getApplicationContext());//初始化 百度地图 要放在setContentView（）之前
        setContentView(R.layout.activity_baidu_map);
        initBaiduMap();
    }

    private void initBaiduMap() {
        mapView= (MapView) findViewById(R.id.mybaiduview);
        baiduMap = mapView.getMap();
        baiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);//普通地图
        // baidumap.setMapType(BaiduMap.MAP_TYPE_SATELLITE);//卫星地图

        baiduMap.setMyLocationEnabled(true);
        locationClient = new LocationClient(this);//getApplicationContext()
        locationClient.registerLocationListener(new MycityLocationListener());
        setLocationOption();
    }

    private void setLocationOption() {
        LocationClientOption locationClientOption = new LocationClientOption();
        locationClientOption.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);//设置模式
        locationClientOption.setOpenGps(true);
        locationClientOption.setCoorType("bd0911");
        locationClientOption.setIsNeedAddress(true);//设置是否需要地址信息，默认不需要,不设置的话bdLocation 为null
        locationClientOption.setScanSpan(5000);//每隔5秒自动更新定位
        locationClient.setLocOption(locationClientOption);
        locationClient.start();
    }

    private boolean isFirstLocate=true;
    private void MoveMyLocation(BDLocation bdLocation){
        if(isFirstLocate){
            LatLng latLng = new LatLng(bdLocation.getLatitude(), bdLocation.getLongitude());
            MapStatusUpdate update = MapStatusUpdateFactory.newLatLng(latLng);
            baiduMap.animateMapStatus(update);
            update = MapStatusUpdateFactory.zoomTo(16f);
            baiduMap.animateMapStatus(update);
            isFirstLocate=false;
        }

        MyLocationData.Builder builder = new MyLocationData.Builder();
        builder.latitude(bdLocation.getLatitude());
        builder.longitude(bdLocation.getLongitude());
        MyLocationData build = builder.build();
        baiduMap.setMyLocationData(build);
    }

    class MycityLocationListener implements BDLocationListener {
        @Override
        public void onReceiveLocation(BDLocation bdLocation) {
            MoveMyLocation(bdLocation);
            String str = bdLocation.getAddrStr();
            //myLocaText.setText(str);
            // bdLocation.getProvince();//获取省份   还有其他获取城市、街道等
        }

    }
}
